
#import "mapViewController.h"
#import "PlacemarkViewController.h"
#import "ephemeraAppDelegate.h"
#import "mintAnnotation.h"
#import "AddressAnnotation.h"
#import "detailViewController.h"

@implementation mapViewController

@synthesize mapView, longDict, latDict, eDict, headlineKeys, buttonTags;

- (void)viewDidLoad
{
    [super viewDidLoad];
	[mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
	

	
	mapView.delegate=self;
	ephemeraAppDelegate *appDelegate = (ephemeraAppDelegate *)[[UIApplication sharedApplication] delegate];
	longDict = appDelegate.longDict;
	latDict = appDelegate.latDict;
	eDict = appDelegate.eDict;

	
	eDict = appDelegate.eDict;
	
	buttonTags = [[NSMutableDictionary alloc] init];
	
	headlineKeys = [eDict allKeys];
	NSLog(@"headlinekeys %@", headlineKeys);

//	mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
	mapView.showsUserLocation=TRUE;

	MKCoordinateRegion region;
	CLLocationCoordinate2D locationSF;
	MKCoordinateSpan span;

	float sfLat = 37.800f;
	float sfLong = -122.410f;
	
	locationSF.longitude = sfLong;
	locationSF.latitude = sfLat;
	
	region.center = locationSF;

	span.latitudeDelta=.020;
	span.longitudeDelta=.020;

	region.span = span;
	
	[self.mapView setRegion:region animated:TRUE];
	
	for(NSString *headlineKey in headlineKeys){
		float theLat = [[latDict objectForKey:headlineKey] doubleValue];
		float theLong = [[longDict objectForKey:headlineKey] doubleValue];
		
		CLLocationCoordinate2D theCoordinate;
		theCoordinate.latitude = theLat;
		theCoordinate.longitude = theLong;
		
		AddressAnnotation *theAnnot = [[AddressAnnotation alloc] initWithCoordinate:theCoordinate andWithText:headlineKey andWithSubtext:[eDict objectForKey:headlineKey]];
		

		
		[self.mapView addAnnotation:theAnnot];
		
	}
		

	[self.view insertSubview:mapView atIndex:0];
	mapView.delegate=self;

}

-(IBAction)viewDetail:(id)sender{
	

	NSString *headline = [buttonTags objectForKey:[NSNumber numberWithInt:[sender tag]]];
	NSLog(@"the sender tag: %d", [sender tag]);
	NSLog(@"the headline: %@", headline);
	detailViewController *dvc = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];
	dvc.selectedHeadline = headline;
	NSLog(@"headline: %@", headline);
	[self.navigationController pushViewController:dvc animated: YES];
	
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
	NSLog(@"in the tap in the mvc");
	
	
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
    MKPinAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
		NSString *headline = [annotation title];
		NSLog(@"the annotation: %@", headline);
		pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:headline];
		if ( pinView == nil )
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:headline] autorelease];
        pinView.pinColor = MKPinAnnotationColorPurple;
	
		UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
		myDetailButton.tag = [buttonTags count];
		[buttonTags setObject:headline  forKey:[NSNumber numberWithInt:[buttonTags count]]];
		NSLog(@"button tags now: %@", buttonTags);
			
		[myDetailButton addTarget:self action:@selector(viewDetail:) forControlEvents:UIControlEventTouchUpInside];
		
		pinView.rightCalloutAccessoryView = myDetailButton;
		pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

		return pinView;
		
    }
    else
        [mapView.userLocation setTitle:@"I am here"];
    return pinView;
}
- (void)viewDidUnload
{
    self.mapView = nil;
}

- (void)dealloc
{
    [mapView release];

    
    [super dealloc];
}



@end
